$(function () {
    //初始化专业/科目
    $.initSubject({
        element:'course-open-subject-box',//初始化专业select元素的ID
        context:'course-open-subject-context',//显示专业/科目名字内容ID
        initData:$('#subjectIds').val(),//初始化显示的专业数据，默认不设置
        removeCallback:function(parentId,subjectIds){//删除回调
            resetSubjectIds()
        },
        confirmCallback:function(parentId,subjectIds){//点击确认回调
            resetSubjectIds()
        }
    });

    /**
     * 重置专业ID串
     */
    function resetSubjectIds() {
        var _subjectIds = $.getAllSubjectIds("course-open-subject-context");
        $("input[name=subjectIds]").val(_subjectIds);
    }

    $('body').on('click', '#saveReturn, #saveNext', function () {
        $(".btn-primary").attr("disabled",'disabled');
        var type = $(this).data('type');
        saveMould(type);
    }).on('click', '#saveCancel', function () { //取消
        window.location = baseloaction + '/admin/course/mould/findMouldList';
    }).on('keyup','input[name="materialNum"]',function () {//监听预估素材数
        if(!/^[0-9]+$/.test(this.value)){
            this.value = 0;
        }
    }).on('click','#secondStep',function(){
        $("#saveNext").click();
    });

    /**
     * 保存模块
     * @param type 1保存返，2保存下一步
     */
    function saveMould(type) {
        var params = $('#mouldStepOne').serializeJSON();
        params= JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/admin/course/mould/saveMould',
            data: params,
            dataType: 'json',
            type:'post',
            contentType:'application/json;charset=utf-8',
            success: function (response) {
                if (response.success) {
                    if (type==1) {//保存返回
                        window.location = baseloaction + '/admin/course/mould/findMouldList';
                    } else if(type==2){//保存下一步
                        var obj = response.entity;
                        //模块ID
                        var mouldId =obj.id;
                        window.location = baseloaction + '/admin/course/mould/initMouldMaterialData?mouldId='+mouldId;
                    }
                } else {
                    $(".btn-primary").removeAttr("disabled");
                    icModale(0, response.message);
                }
            },
            error: function () {
                $(".btn-primary").removeAttr("disabled");
                icModale(0, '系统错误，保存模块失败');
            }
        });
    }
});